package xcmg.device.web.agent;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.dao.entity.agent.ServiceBorrowInDetailDO;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.agent.ServiceBorrowInDetailService;
import xcmg.device.service.vo.agent.ServiceBorrowInDetailVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.List;

/**
 * ServiceBorrowInDetail Controller 实现类
 *
 * @author huhuawei
 */
@RestController
@RequestMapping("/serviceBorrowInDetail")
public class ServiceBorrowInDetailController extends BasicController {
    private static final Logger logger = LoggerFactory.getLogger(ServiceBorrowInDetailController.class);
    @Autowired
    private ServiceBorrowInDetailService serviceBorrowInDetailService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "服务退回单明细分页列表信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<ServiceBorrowInDetailVO>> list(@RequestBody SearchCommonVO<ServiceBorrowInDetailVO> condition) {
        PageCommonVO<ServiceBorrowInDetailVO> pageCommonVO = serviceBorrowInDetailService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "服务退回单明细-保存")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> save(@RequestBody ServiceBorrowInDetailVO serviceBorrowInDetailVO) {
        if (null == serviceBorrowInDetailVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            ErrorCode code = serviceBorrowInDetailService.save(serviceBorrowInDetailVO, getUserId(), getUserName(), getOrgId());
            return actionResult(code);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询服务退回单明细详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult<ServiceBorrowInDetailVO> item(String id) {
        ServiceBorrowInDetailDO serviceBorrowInDetailDO = serviceBorrowInDetailService.queryById(id);
        ServiceBorrowInDetailVO serviceBorrowInDetailVO = new ServiceBorrowInDetailVO();
        BeanUtils.copyProperties(serviceBorrowInDetailDO, serviceBorrowInDetailVO);
        return actionValueResult(serviceBorrowInDetailVO);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除服务退回单明细数据")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> delete(@RequestBody List<String> ids) {
        if (null == ids || ids.isEmpty()){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode;
        try {
            errorCode = serviceBorrowInDetailService.deleteDetails(ids,getOrgId());
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }
}
